package com.example.twitterfake;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class TrendTops extends Activity {
	
	ListView lv_trendtops;
	String resultado;
	
	@Override
	public void onCreate(Bundle savedInstanceState)  {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trendtops);
		
		Button btsair1 = (Button) findViewById(R.id.btsair1);
		
		btsair1.setOnClickListener(new View.OnClickListener(){
			public void onClick(View arg0) {
				Intent login = new Intent(TrendTops.this,Login.class);
				TrendTops.this.startActivity(login);
				TrendTops.this.finish();	
			}
		});
		
		try {
			resultado = Rest.getTrendTops();
			System.out.println(resultado);
			
			List<String> trendtops = listTrendtops (resultado);		
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, trendtops);
			lv_trendtops = (ListView) findViewById(R.id.lv_TrendTops);
			lv_trendtops.setAdapter(adapter);
			
			lv_trendtops.setOnItemClickListener (new OnItemClickListener() {
	            public void onItemClick (AdapterView parent, View view, int position, long id) {
	            	
	            	String valor = ((TextView) view).getText().toString().trim();
	            	System.out.println(valor);
	            	
	            	try {
						String posts = ListarPosts (resultado, ((TextView) view).getText().toString().trim());
						AlertDialog.Builder mensagem = new AlertDialog.Builder(TrendTops.this);
		                mensagem.setTitle(((TextView) view).getText().toString());
						mensagem.setMessage(posts);
						mensagem.setNeutralButton("OK", null);
			            mensagem.show();
					} catch (JsonProcessingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}       
	 
	            }
	        });
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	 public List<String> listTrendtops (String string) throws JSONException, JsonProcessingException, IOException {
		
		 List<String> trendtops = new ArrayList<String>();
		 ObjectMapper mapper = new ObjectMapper();  
		 JsonNode dados = mapper.readTree(string);
		 
		 
		 int i = 0;
		 while (dados.get(i) != null) {
			 JsonNode n = dados.get(i);
			 trendtops.add(n.get("name").getValueAsText());
			 i++;
		 }
		 
		 return trendtops;
	 }
	 
	 public String ListarPosts (String string, String valor) throws JSONException, JsonProcessingException, IOException {
			
		 List <String>ids = new ArrayList<String>();
		 String posts = new String ();
		 ObjectMapper mapper = new ObjectMapper();  
		 JsonNode dados = mapper.readTree(string);
		 
		 int i = 0;
		 while (dados.get(i) != null) {
			 JsonNode n = dados.get(i);
			 if (n.get("name").getValueAsText().equals(valor)){
				 JsonNode post_ids = n.get("post_ids");
				 
				 int k = 0;
				 while (post_ids.get(k) != null) {
					 ids.add(post_ids.get(k).getValueAsText());
					 k++;
				 }
				 break;
			 	}
			 i++;	 
		 }
		 
		 for (int j = 0; j < ids.size(); j++){
			 String post = Rest.getPostsId(ids.get(j))+"\n";
			 
			 ObjectMapper psts = new ObjectMapper();  
			 JsonNode p = psts.readTree(post);
			 
			 posts += ". "+p.get("body").getValueAsText()+"\n";

		 }
		
		 System.out.println(posts);
		 return posts;
	 }

	 
}
